package com.education;

import java.io.IOException;
import java.util.BitSet;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.audio.sound.SoundManager;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.svg.opengl.texture.atlas.bitmap.SVGBitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;

public class Alphabet {
	
	public static final int[] NORMAL_VOWEL     = {20,28,0,15,30}; //{у, ы, а, о, э}
	public static final int[] NORMAL_CONSONANT = {11,14,3,8,22,21,2,16,17,12,4,18,13,19,1}; //{к, н, г, з, х, ф, в, п, р, л, д, с, м, т, б}
	public static final int[] STRANGE_VOWEL   = {5,6,31,32}; //{е,ё,ю,я}
	public static final int[] HARDEST   = {7,25,23}; //['ж', 'ш', 'ц']
	public static final int[] SOFTEST   = {24,26,10}; //['ч', 'щ', 'й']
	
	
	public final static String badSound = "badSound.mp3"; //йи
	public final static String[] alphabetSounds = {"letter0.mp3","letter1.mp3","letter2.mp3", //0-32
	                                               "letter3.mp3","letter4.mp3","letter5.mp3",
	                                               "letter6.mp3","letter7.mp3","letter8.mp3",
	                                               "letter9.mp3","letter10.mp3","letter11.mp3",
	                                               "letter12.mp3","letter13.mp3","letter14.mp3",
	                                               "letter15.mp3","letter16.mp3","letter17.mp3",
	                                               "letter18.mp3","letter19.mp3","letter20.mp3",
	                                               "letter21.mp3","letter22.mp3","letter23.mp3",
	                                               "letter24.mp3","letter25.mp3","letter26.mp3",
	                                               "letter27.mp3","letter28.mp3","letter29.mp3",
	                                               "letter30.mp3","letter31.mp3","letter32.mp3"};
	
	public final static String[][] sounds = {
		//	а 0
		{"sound0.mp3","sound0.mp3"},
		//	б 1
		{"sound1.mp3","altsound1.mp3"},
		//	в 2
		{"sound2.mp3","altsound2.mp3"},
		//	г 3
		{"sound3.mp3","altsound3.mp3"},
		//	д 4
		{"sound4.mp3","altsound4.mp3"},
		//	е 5
		{"sound5.mp3","altsound5.mp3"},
		//	ё 6
		{"sound6.mp3","altsound6.mp3"},
		//	ж 7
		{"sound7.mp3","sound7.mp3"},
		//	з 8
		{"sound8.mp3","altsound8.mp3"},
		//	и 9
		{"sound9.mp3","sound9.mp3"},
		//	й 10
		{"sound10.mp3","sound10.mp3"},
		//	к 11
		{"sound11.mp3","altsound11.mp3"},
		//	л 12
		{"sound12.mp3","altsound12.mp3"},
		//	м 13
		{"sound13.mp3","altsound13.mp3"},
		//	н 14
		{"sound14.mp3","altsound14.mp3"},
		//	о 15
		{"sound15.mp3","sound15.mp3"},
		//	п 16
		{"sound16.mp3","altsound16.mp3"},
		//	р 17
		{"sound17.mp3","altsound17.mp3"},
		//	с 18
		{"sound18.mp3","altsound18.mp3"},
		//	т 19
		{"sound19.mp3","altsound19.mp3"},
		//	у 20
		{"sound20.mp3","sound20.mp3"},
		//	ф 21
		{"sound21.mp3","altsound21.mp3"},
		//	х 22
		{"sound22.mp3","altsound22.mp3"},
		//	ц 23
		{"sound23.mp3","sound23.mp3"},
		//	ч 24
		{"sound24.mp3","sound24.mp3"},
		//	ш 25
		{"sound25.mp3","sound25.mp3"},
		//	щ 26
		{"sound26.mp3","sound26.mp3"},
		//	ъ 27
		{"sound27.mp3","sound27.mp3"},//TODO придумать что читать как звук Ъ
		//	ы 28
		{"sound28.mp3","sound28.mp3"},
		//	ь 29
		{"sound29.mp3","sound29.mp3"},//TODO придумать что читать как звук Ь
		//	э 30
		{"sound30.mp3","sound30.mp3"},
		//	ю 31
		{"sound31.mp3","altsound31.mp3"},
		//	я 32
		{"sound32.mp3","altsound32.mp3"}
	};

//	0	{}
//	1	normal_c = {к, н, г, з, х, ф, в, п, р, л, д, с, м, т, б}
//	2	normal_v={у, ы, а, о, э}
//	3	hardest = {ш, ж, ц}
//	4	softest = {щ, й, ч}
//	5	strange_v = {е,ё,ю,я}
//	6	{и}
//	7	{ь}
//	8	{ъ}
	public static final int[][][] alphabetEngineTabel = 
	{
		{{0,0,0,0,0,0,0,0,0},{8704,8768,8768,8768,8768,13900,13900,13888,8768},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{8704,8768,8768,8768,8768,10316,10318,10304,8768},{8704,8768,8768,8768,8768,8268,8268,8256,8768},{8704,8768,8768,8768,8768,8768,8768,8768,8768},{8704,8768,8768,8768,8768,8768,8768,8768,8768},{8704,8256,8256,8256,8256,8256,8271,8256,8256},{8704,8768,8768,8768,8768,10304,10304,10304,8768}},{{0,0,0,0,0,0,0,0,0},{1057344,1057344,1057344,1057344,1057344,1062476,1062476,1062464,1057344},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057344,1057344,1057344,1057344,1057344,1058892,1058894,1058880,1057344},{1057344,1057344,1057344,1057344,1057344,8268,8268,1056832,1057344},{1779200,1779264,1779264,1779264,1779264,1779264,1779264,1779264,1779264},{1779200,1779264,1779264,1779264,1779264,1779264,1779264,1779264,1779264},{1777664,1777728,1777728,1777728,1777728,1777728,1777743,1777728,1777728},{1057344,1057344,1057344,1057344,1057344,1058880,1058880,1058880,1057344}},{{0,0,0,0,0,0,0,0,0},{1057344,1057344,1057344,1057344,1057344,1062476,1062476,1062464,1057344},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057344,1057344,1057344,1057344,1057344,1058892,1058894,1058880,1057344},{1057344,1057344,1057344,1057344,1057344,8268,8268,1056832,1057344},{1057280,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057280,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1056768,1056832,1056832,1056832,1056832,1056832,1056832,1056832,1056832},{1057344,1057344,1057344,1057344,1057344,1058880,1058880,1058880,1057344}},{{0,0,0,0,0,0,0,0,0},{1057344,1057344,1057344,1057344,1057344,1062476,1062476,1062464,1057344},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057344,1057344,1057344,1057344,1057344,1058892,1058894,1058880,1057344},{1057344,1057344,1057344,1057344,1057344,8268,8268,1056832,1057344},{1320448,1320512,1320512,1320512,1320512,1320512,1320512,1320512,1320512},{1320704,1320768,1320768,1320768,1320768,1320768,1320768,1320768,1320768},{1318912,1318976,1318976,1318976,1318976,1318976,1318991,1318976,1318976},{1057344,1057344,1057344,1057344,1057344,1058880,1058880,1058880,1057344}},{{0,0,0,0,0,0,0,0,0},{1057344,1057344,1057344,1057344,1057344,1062476,1062476,1062464,1057344},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057344,1057344,1057344,1057344,1057344,1058892,1058894,1058880,1057344},{1057344,1057344,1057344,1057344,1057344,8268,8268,1056832,1057344},{1058304,1058368,1058368,1058368,1058368,1058368,1058368,1058368,1058368},{1058304,1058368,1058368,1058368,1058368,1058368,1058368,1058368,1058368},{1056768,1056832,1056832,1056832,1056832,1056832,1056847,1056832,1056832},{1057344,1057344,1057344,1057344,1057344,1058880,1058880,1058880,1057344}},{{0,0,0,0,0,0,0,0,0},{1057344,1057344,1057344,1057344,1057344,1062476,1062476,1062464,1057344},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057344,1057344,1057344,1057344,1057344,1058892,1058894,1058880,1057344},{1057344,1057344,1057344,1057344,1057344,8268,8268,1056832,1057344},{1057280,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057280,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1056768,1056832,1056832,1056832,1056832,1056832,1056832,1056832,1056832},{1057344,1057344,1057344,1057344,1057344,1058880,1058880,1058880,1057344}},{{0,0,0,0,0,0,0,0,0},{1057344,1057344,1057344,1057344,1057344,1062476,1062476,1062464,1057344},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057344,1057344,1057344,1057344,1057344,1058892,1058894,1058880,1057344},{1057344,1057344,1057344,1057344,1057344,8268,8268,1056832,1057344},{1779200,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1779200,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1777664,1056832,1056832,1056832,1056832,1056832,1056832,1056832,1056832},{1057344,1057344,1057344,1057344,1057344,1058880,1058880,1058880,1057344}},{{0,0,0,0,0,0,0,0,0},{1057344,1057344,1057344,1057344,1057344,1062476,1062476,1062464,1057344},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057344,1057344,1057344,1057344,1057344,1058892,1058894,1058880,1057344},{1057344,1057344,1057344,1057344,1057344,8268,8268,1056832,1057344},{1057280,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1058688,1058752,1058752,1058752,1058752,1058752,1058752,1058752,1058752},{1056768,1056832,1056832,1056832,1056832,1056832,1056832,1056832,1056832},{1057344,1057344,1057344,1057344,1057344,1058880,1058880,1058880,1057344}},{{0,0,0,0,0,0,0,0,0},{1057344,1057344,1057344,1057344,1057344,1062476,1062476,1062464,1057344},{1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344,1057344},{1057344,1057344,1057344,1057344,1057344,1058892,1058894,1058880,1057344},{1057344,1057344,1057344,1057344,1057344,8268,8268,1056832,1057344},{1319424,1319488,1319488,1319488,1319488,1319488,1319488,1319488,1319488},{1319424,1319488,1319488,1319488,1319488,1319488,1319488,1319488,1319488},{1319424,1319488,1319488,1319488,1319488,1319488,1319488,1319488,1319488},{1057344,1057344,1057344,1057344,1057344,1058880,1058880,1058880,1057344}}
	};
	
	public static ITextureRegion[] mLettersTextureRegions = new ITextureRegion[33];
	public static ITiledTextureRegion mSoftAnimSVGTexture;
	
	public Alphabet(MainActivity activity)
	{
		
		//SVGBitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		BuildableBitmapTextureAtlas mBuildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 2048, 1024, TextureOptions.NEAREST);
		Alphabet.mSoftAnimSVGTexture = SVGBitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBuildableBitmapTextureAtlas, activity, "soft_anim.svg", (int)(752*activity.scaleRatio), (int)(323*activity.scaleRatio), 4, 3 );
		//this.mLettersTextureRegions = new ITextureRegion[33];
		for(int i=0;i<=32;i++)
		{
			Alphabet.mLettersTextureRegions[i] = SVGBitmapTextureAtlasTextureRegionFactory.createFromAsset(mBuildableBitmapTextureAtlas, activity, i+"_letter.svg", (int)(195*activity.scaleRatio), (int)(278*activity.scaleRatio));
		}
		
		try {
			mBuildableBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(1, 1, 1));
		} catch (TextureAtlasBuilderException e) {
			// TODO TextureAtlasBuilderException Auto-generated catch block
			e.printStackTrace();
		}
		mBuildableBitmapTextureAtlas.load();
		
		//SOUND
//		try
//		{
//			//successSound = SoundFactory.createSoundFromAsset(this.getSoundManager(), this.getApplicationContext(), "snd/Clapping.mp3");
//			//TODO: добавить остальные звуки
//		} catch (IOException e)
//		{
//			e.printStackTrace();
//		}
	}
	
	public static Sound getAlphabetSound(MainActivity activity, int letterID)
	{
		try {
			return SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity.getApplicationContext(),Alphabet.alphabetSounds[letterID]);
		} catch (IOException e) {
			// TODO getAlphabetSound IOException Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static Sound getSound(final MainActivity activity, int letterID, boolean isAlt)
	{
		return getSound(activity, letterID, isAlt, -1);
	}
	
	public static Sound getSound(final MainActivity activity, int letterID, boolean isAlt, int badIndex)
	{
		try
		{
			if (badIndex==1)
			{
				//йи
				return SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity.getApplicationContext(),Alphabet.badSound);
			} else if (badIndex==2)
			{
				//ы
				return SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity.getApplicationContext(),Alphabet.sounds[28][0]);
			}
			if (!isAlt)
			{
				return SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity.getApplicationContext(),Alphabet.sounds[letterID][0]);
			} else
			{
				return SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity.getApplicationContext(),Alphabet.sounds[letterID][1]);
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;//O_O
	}
}
